package com.panopset.flywheel;

import com.panopset.Util;

/**
 * Flywheel control flags.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Control {

    /**
     * Auto-step wait time.
     */
    public static final int AUTO_STEP_WAIT_TIME = 500;

    /**
     * Step resolve a command, in Flywheel debug mode.
     *
     * @param command
     *            Command to step resolve.
     */
    void stepResolve(final Command command) {
        setResolvingCommand(command);
        if (isStepping()) {
            while (isWaiting()) {
                try {
                    Thread.sleep(AUTO_STEP_WAIT_TIME);
                } catch (InterruptedException ex) {
                    setWaiting(false);
                }
                if (isStopped()) {
                    setStepping(false);
                    setWaiting(false);
                }
            }
            setWaiting(true);
            setResolvingCommand(null);
        }
    }

    /**
     * Resolving command.
     */
    private Command resolvingCommand;

    /**
     * @return Currently resolving command.
     */
    public Command getResolvingCommand() {
        return resolvingCommand;
    }

    /**
     * Set resolving command.
     *
     * @param command
     *            Resolving command.
     */
    public void setResolvingCommand(final Command command) {
        resolvingCommand = command;
        if (Util.isVerbose()) {
            Util.dspmsg(Util.x("Executing") + " " + command.getDescription());
        }
    }

    /**
     * @return true if replacements are suppressed.
     */
    public boolean isReplacementsSuppressed() {
        return replacementsSuppressed;
    }

    /**
     * Used to temporarily suppress replacements.
     *
     * @param value
     *            Replacements suppressed.
     */
    public void setReplacementsSuppressed(final boolean value) {
        this.replacementsSuppressed = value;
    }

    /**
     * @return Stop execution flag.
     */
    public boolean isStopped() {
        return stop;
    }

    /**
     * Some error conditions require that Flywheel execution must stop.
     */
    public void stop() {
        stop = true;
    }

    /**
     * @return true if stepping through flywheel commands.
     */
    public boolean isStepping() {
        return stepping;
    }

    /**
     * @param value
     *            Stepping through commands flag.
     */
    public void setStepping(final boolean value) {
        this.stepping = value;
    }

    /**
     *
     * @return true if waiting for user to step to the next command.
     */
    public boolean isWaiting() {
        return waiting;
    }

    /**
     * @param value
     *            Set waiting for next command flag.
     */
    public void setWaiting(final boolean value) {
        this.waiting = value;
    }

    /**
     * Stop flag.
     */
    private boolean stop = false;

    /**
     * Stepping flag.
     */
    private boolean stepping = false;

    /**
     * Waiting flag.
     */
    private boolean waiting = false;

    /**
     * Replacement suppression flag.
     */
    private boolean replacementsSuppressed = false;
}
